package ch.sv7.tool.classloader;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ClassFinder {

	public static String[] getClassesImplementing(Class interfaceClass, String filename) {
		List result = new ArrayList();
		List classNames = new ArrayList();
		try {
			ZipInputStream zis = new ZipInputStream(new FileInputStream(filename));
			for (ZipEntry ze = null; (ze = zis.getNextEntry()) != null;)
				if (!ze.isDirectory() && ze.getName().endsWith(".class") && ze.getName().indexOf("$") == -1) {
					String entryPath = ze.getName();
					classNames.add(entryPath.substring(0, entryPath.length() - ".class".length()).replace('/', '.'));
				}

			ZipClassLoader classLoader = new ZipClassLoader(filename, new Vector());
			for (int i = 0; i < classNames.size(); i++) {
				String className = (String) classNames.get(i);
				try {
					Class clazz = classLoader.loadClass(className);
					Class interfaces[] = clazz.getInterfaces();
					for (int j = 0; j < interfaces.length; j++)
						if (interfaces[j].equals(interfaceClass))
							result.add(className);

				} catch (Throwable throwable) {
				}
			}

		} catch (Exception exception) {
		}
		return (String[]) result.toArray(new String[0]);
	}

}
